/**
 *  Copyright 2002-2009 the original author or authors.
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */
package foo.bar.wiki.services;

import org.springframework.context.ApplicationContext;

/**
 * Services as a static point to retrieve various services eg. {@link foo.bar.wiki.services.UserService},
 * {@link UserExtendedWildcardPermissionService} etc. The best option would be to have services injected
 * through spring's configuration. However in some cases this might be impossible, in those cases these
 * service class serves as the entry point to obtained those services.
 * <p/>
 * The services in this static util class will be setup upon successfull initialization of Spring's
 * application context, throuhg {@link foo.bar.wiki.services.SetupService} wich implements
 * {@link org.springframework.context.ApplicationListener}
 *
 * @see {@link foo.bar.wiki.services.SetupService#onStartup()}
 *
 * @author tmjee
 * @version $Date$ $Id$
 */
public class ServicesUtil {

    private static UserExtendedWildcardPermissionService service;
    private static UserService userService;
    private static WikiService wikiService;

    private static ApplicationContext applicationContext;


    /**
     * Set during startup in SetupService (which boots up upon Spring's Application COntext initialization)
     * @param applicationContext
     */
    static void setApplicationContext(ApplicationContext applicationContext) {
        ServicesUtil.applicationContext = applicationContext;
    }




    // =========================

    public static UserExtendedWildcardPermissionService getUserExtendedWildcardPermissionService() {
        if (service == null) {
            service =  (UserExtendedWildcardPermissionService) applicationContext.getBean("userExtendedWildcardPermissionService");
        }
        return service;
    }


    public static UserService getUserService() {
        if (userService == null) {
            userService = (UserService) applicationContext.getBean("userService");
        }
        return userService;
    }

    public static WikiService getWikiService() {
        if (wikiService == null) {
            wikiService = (WikiService) applicationContext.getBean("wikiService");
        }
        return wikiService;
    }

}


